package com.wyf.cnbeta;

import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutCompat;
import android.text.Html;
import android.util.Log;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.VideoView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.umeng.analytics.MobclickAgent;


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;

public class NewsActivity extends AppCompatActivity {

    String url,name,time,text;
    ArrayList<String> text_litst=new ArrayList<String>();
    TextView title_text,time_text;
    LinearLayout lin1;
    int size;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);
        MobclickAgent.setScenarioType(this, MobclickAgent.EScenarioType.E_UM_NORMAL);
        url=getIntent().getStringExtra("href").toString();
        lin1=(LinearLayout)findViewById(R.id.line1_tt);
        new GetNews().execute();

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);


    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("SplashScreen");
        MobclickAgent.onResume(this);

    }
    public void onPause() {
        super.onPause();
        super.onPause();
        MobclickAgent.onPageEnd("SplashScreen");
        MobclickAgent.onPause(this);
    }

    public boolean onOptionsItemSelected(MenuItem item)
    {
        // TODO Auto-generated method stub
        if(item.getItemId() == android.R.id.home)
        {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }





    class  GetNews extends AsyncTask{
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Object doInBackground(Object[] params) {
            try {
                Document doc= Jsoup.connect(url).get();
                name=doc.getElementsByClass("title").text();


                Log.d("TAG",name);
                time=doc.getElementsByClass("time").text();
                Log.d("TAG",time);
                Elements item=doc.getElementsByClass("content").select("p");
                for(Element i:item)
                {

                    size=item.size();
                    if(!(i.select("img").isEmpty()))
                    {
                        text=i.select("img[src]").attr("src").toString();
                        text_litst.add(text);
                        Log.d("TAG","图片链接："+text);

                    }
                    else if(!(i.getElementsByClass("edui-faked-video").isEmpty()))
                    {
                        text=i.getElementsByClass("edui-faked-video").attr("src");
                        text_litst.add("edui-faked-video"+text);
                        Log.d("TAG","视频链接："+text);

                    }
                    else {
                        text="<br>"+i.html();
                        text_litst.add(text);
                        Log.d("TAG","文本："+text);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }


        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);
            Typeface typeface=Typeface.create("黑体",Typeface.BOLD);

            title_text=(TextView) findViewById(R.id.title_text);
            title_text.setText(name);
            title_text.setTypeface(typeface);
            time_text=(TextView) findViewById(R.id.time_text);
            time_text.setText(time);
            time_text.setTypeface(typeface);
            String temp;
            for(int i=0;i<text_litst.size();i++)
            {
                temp=text_litst.get(i);
                if(temp.contains("http://static.cnbetacdn.com"))
                {
                    final ImageView imageView=new ImageView(NewsActivity.this);

//                    Glide.with(NewsActivity.this).load(temp).diskCacheStrategy(DiskCacheStrategy.ALL).thumbnail(0.1f).into(imageView);
                    RequestQueue mQueue = Volley.newRequestQueue(NewsActivity.this);
                    final String finalTemp = temp;
                    ImageRequest imageRequest=new ImageRequest(temp, new Response.Listener<Bitmap>() {
                        @Override
                        public void onResponse(Bitmap bitmap) {
                            imageView.setImageBitmap(bitmap);

                        }
                    }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError volleyError) {
                            imageView.setImageResource(R.mipmap.ic_launcher);

                        }
                    });
                    mQueue.add(imageRequest);

                    LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.MATCH_PARENT, LinearLayoutCompat.LayoutParams.MATCH_PARENT);

                    lin1.addView(imageView,lp1);
                }
                else if(temp.contains("edui-faked-video"))
                {
                    temp=temp.replaceFirst("edui-faked-video","");
                    Uri uri = Uri.parse(temp);
                    VideoView videoView=new VideoView(NewsActivity.this);
//                    MediaController mediaController=new MediaController(NewsActivity.this);
//                    videoView.setMediaController(mediaController);
                    videoView.setVideoURI(uri);
                    //videoView.start();
                    videoView.requestFocus();
                    LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.MATCH_PARENT, LinearLayoutCompat.LayoutParams.MATCH_PARENT);
                    lin1.addView(videoView,lp1);
                }
                else {
                    TextView tv=new TextView(NewsActivity.this);

                    tv.setText(Html.fromHtml(temp));
                    LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.MATCH_PARENT, LinearLayoutCompat.LayoutParams.MATCH_PARENT);
                    lin1.addView(tv,lp1);


                }
            }

        }
    }


}
